// pages/mine/info/info.js
var that={}
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      phone:'',
      name:'',
      headpic:'',

      userinfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({ title: '个人信息' })
    that=this;
    var uid = wx.getStorageSync('uid');
    wx.request({
      url:app.globalData.baseUrl +'/user/getUserInfo', //仅为示例，并非真实的接口地址
      // url: app.globalData.url.login,
      method: 'post',
      data: {
        id: uid,      

      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.code == 0) {
          that.setData({

            userinfo: res.data.data, //请求结果数据
            headpic:res.data.data.img

          })
         
        }

      }
    })
  },
  getInputPhoneValue(e) {
    that.setData({

      phone: e.detail.value

    })
  },
  getInputNameValue(e) {
    that.setData({

      name: e.detail.value

    })
  },
  chooseImage: function () {
    
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        // console.log(res)
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths[0])
        wx.uploadFile({
          url: app.globalData.baseUrl +'/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {},
          formData: {},
          success: function (res) { 
           
            console.log(res.data)
            let temp =  JSON.parse(res.data);
            var img = app.globalData.baseUrl +'/'+ temp.data;
            console.log(img);
            that.setData({
              headpic:img,
            })
          },
          fail: function (res) { },
          complete: function (res) { },
        })
       
      
     
       
      }
    })
  },
  upInfo:function(e){
    var uid = wx.getStorageSync('uid');
    wx.request({
      url:app.globalData.baseUrl +'/user/upUser', //仅为示例，并非真实的接口地址
      // url: app.globalData.url.login,
      method: 'post',
      data: {
        id: uid,
        phone:that.data.phone,
        img:that.data.headpic,     
        name:that.data.name,

      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.msg,
           
          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})